//
//  SISSMapPriceBottomShowView.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/11/30.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSMapPriceBottomShowView.h"


@interface SISSMapPriceBottomShowView ()

@property (weak, nonatomic) UILabel *currentRegionTitleLabel;
@property (weak, nonatomic) UILabel *currentRegionLabel;
@property (weak, nonatomic) UILabel *countLabel;
@property (weak, nonatomic) UIButton *detailsButton;
@property (weak, nonatomic) UIView *middleLine;
@property (weak, nonatomic) UIImageView *sellImageView;
@property (weak, nonatomic) UILabel *sellRingRatioLabel;
@property (weak, nonatomic) UILabel *sellPriceTitleLabel;
@property (weak, nonatomic) UILabel *sellPriceLabel;
@property (weak, nonatomic) UIImageView *rentImageView;
@property (weak, nonatomic) UILabel *rentPriceTitleLabel;
@property (weak, nonatomic) UILabel *rentPriceLabel;
@property (weak, nonatomic) UILabel *rentRingRatioLabel;

@end
@implementation SISSMapPriceBottomShowView

- (void)setTownInfoDetail:(SISSMapTownInfoDetailModel *)townInfoDetail {
    _townInfoDetail = townInfoDetail;
    if (!townInfoDetail) return;
    self.detailsButton.userInteractionEnabled = YES;
    self.detailsButton.tag = 100;
    self.currentRegionLabel.text = townInfoDetail.townName;
    NSString *countStr = [NSString stringWithFormat:@"此区域内共有%@个板块",townInfoDetail.blockCount];
    NSMutableAttributedString *countAttri = [[NSMutableAttributedString alloc] initWithString:countStr];
    NSRange range = [countStr rangeOfString:townInfoDetail.blockCount];
    [countAttri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    self.countLabel.attributedText = countAttri;
    
    NSMutableAttributedString *sellPriceAttri = [[NSMutableAttributedString alloc] initWithString:townInfoDetail.sellPrice];
    [sellPriceAttri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,townInfoDetail.sellPrice.length - @"元/㎡".length)];
    self.sellPriceLabel.attributedText = sellPriceAttri;
    
    if ([[townInfoDetail.sellRingRatio substringToIndex:1] isEqualToString:@"-"]) {
        
        self.sellRingRatioLabel.textColor = [UIColor greenColor];
        self.sellRingRatioLabel.text = [NSString stringWithFormat:@"%@↓",[townInfoDetail.sellRingRatio substringFromIndex:1]];
    } else if ([townInfoDetail.sellRingRatio integerValue] == 0) {
        self.sellRingRatioLabel.textColor = [UIColor blueColor];
        self.sellRingRatioLabel.text = @"持平";
    } else {
        self.sellRingRatioLabel.textColor = [UIColor redColor];
        self.sellRingRatioLabel.text = [NSString stringWithFormat:@"%@↑",townInfoDetail.sellRingRatio];
    }
    
    NSMutableAttributedString *rentPriceAttri = [[NSMutableAttributedString alloc] initWithString:townInfoDetail.rentPrice];
    [rentPriceAttri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,townInfoDetail.rentPrice.length - @"元/月/㎡".length)];
    self.rentPriceLabel.attributedText = rentPriceAttri;
    
    if ([[townInfoDetail.rentRingRatio substringToIndex:1] isEqualToString:@"-"]) {
        
        self.rentRingRatioLabel.textColor = [UIColor greenColor];
        self.rentRingRatioLabel.text = [NSString stringWithFormat:@"%@↓",[townInfoDetail.rentRingRatio substringFromIndex:1]];
    } else if ([townInfoDetail.rentRingRatio integerValue] == 0) {
        self.rentRingRatioLabel.textColor = [UIColor blueColor];
        self.rentRingRatioLabel.text = @"持平";
    } else {
        self.rentRingRatioLabel.textColor = [UIColor redColor];
        self.rentRingRatioLabel.text = [NSString stringWithFormat:@"%@↑",townInfoDetail.rentRingRatio];
    }
}

- (void)setBlockInfoDetail:(SISSMapBlockInfoDetailModel *)blockInfoDetail {
    _blockInfoDetail = blockInfoDetail;
    if (!blockInfoDetail) return;
    self.detailsButton.userInteractionEnabled = YES;
    self.detailsButton.tag = 101;
    self.currentRegionLabel.text = blockInfoDetail.blockName;
    NSString *countStr = [NSString stringWithFormat:@"此板块内共有楼盘%@个",blockInfoDetail.buildsCount];
    NSMutableAttributedString *countAttri = [[NSMutableAttributedString alloc] initWithString:countStr];
    NSRange range = [countStr rangeOfString:blockInfoDetail.buildsCount];
    [countAttri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    self.countLabel.attributedText = countAttri;
    
    NSMutableAttributedString *sellPriceAttri = [[NSMutableAttributedString alloc] initWithString:blockInfoDetail.sellPrice];
    [sellPriceAttri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,blockInfoDetail.sellPrice.length - @"元/㎡".length)];
    self.sellPriceLabel.attributedText = sellPriceAttri;
    
    if ([[blockInfoDetail.sellRingRatio substringToIndex:1] isEqualToString:@"-"]) {
        
        self.sellRingRatioLabel.textColor = [UIColor greenColor];
        self.sellRingRatioLabel.text = [NSString stringWithFormat:@"%@↓",[blockInfoDetail.sellRingRatio substringFromIndex:1]];
    } else if ([blockInfoDetail.sellRingRatio integerValue] == 0) {
        self.sellRingRatioLabel.textColor = [UIColor blueColor];
        self.sellRingRatioLabel.text = @"持平";
    } else {
        self.sellRingRatioLabel.textColor = [UIColor redColor];
        self.sellRingRatioLabel.text = [NSString stringWithFormat:@"%@↑",blockInfoDetail.sellRingRatio];
    }
    
    NSMutableAttributedString *rentPriceAttri = [[NSMutableAttributedString alloc] initWithString:blockInfoDetail.rentPrice];
    [rentPriceAttri addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,blockInfoDetail.rentPrice.length - @"元/月/㎡".length)];
    self.rentPriceLabel.attributedText = rentPriceAttri;
    
    if ([[blockInfoDetail.rentRingRatio substringToIndex:1] isEqualToString:@"-"]) {
        
        self.rentRingRatioLabel.textColor = [UIColor greenColor];
        self.rentRingRatioLabel.text = [NSString stringWithFormat:@"%@↓",[blockInfoDetail.rentRingRatio substringFromIndex:1]];
    } else if ([blockInfoDetail.rentRingRatio integerValue] == 0) {
        self.rentRingRatioLabel.textColor = [UIColor blueColor];
        self.rentRingRatioLabel.text = @"持平";
    } else {
        self.rentRingRatioLabel.textColor = [UIColor redColor];
        self.rentRingRatioLabel.text = [NSString stringWithFormat:@"%@↑",blockInfoDetail.rentRingRatio];
    }
}

/// 点击详情
- (void)detailsButtonClickAction:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(detailsButtonClickActionWithTownNo:blockNo:)]) {
        if (button.tag == 100) {
            [self.delegate detailsButtonClickActionWithTownNo:self.townInfoDetail.townNo blockNo:@""];
        } else {
            [self.delegate detailsButtonClickActionWithTownNo:@"" blockNo:self.blockInfoDetail.blockNo];
        }
    }
}

- (void)initUI {
    [super initUI];
    
    UILabel *currentRegionTitleLabel = [UILabel yyr_labelWithText:@"当前区域：" fontSize:13 textColor:SECONDARY_TEXT_COLOR];
    [self addSubview:currentRegionTitleLabel];
    self.currentRegionTitleLabel = currentRegionTitleLabel;
    
    UILabel *currentRegionLabel = [UILabel yyr_labelWithText:@"" fontSize:13 textColor:MAIN_TEXT_COLOR];
    [self addSubview:currentRegionLabel];
    self.currentRegionLabel = currentRegionLabel;
    
    UILabel *countLabel = [UILabel yyr_labelWithText:@"" fontSize:11 textColor:SECONDARY_TEXT_COLOR];
    [self addSubview:countLabel];
    self.countLabel = countLabel;
    
    UIButton *detailsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [detailsButton setTitle:@"详情" forState:UIControlStateNormal];
    [detailsButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    [detailsButton setImage:[UIImage imageNamed:@"btn_list_more"] forState:UIControlStateNormal];
    detailsButton.titleLabel.font = [UIFont systemFontOfSize:11];
    [detailsButton sizeToFit];
    detailsButton.titleEdgeInsets = UIEdgeInsetsMake(0, -detailsButton.imageView.frame.size.width - detailsButton.frame.size.width + detailsButton.titleLabel.frame.size.width + 10, 0, 0);
    detailsButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -detailsButton.titleLabel.frame.size.width - detailsButton.frame.size.width + detailsButton.imageView.frame.size.width - 10);
    [detailsButton addTarget:self action:@selector(detailsButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:detailsButton];
    self.detailsButton = detailsButton;
    self.detailsButton.userInteractionEnabled = NO;
    
    UIView *middleLine = [[UIView alloc] init];
    middleLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:middleLine];
    self.middleLine = middleLine;
    
    UIImageView *sellImageView = [[UIImageView alloc] init];
    sellImageView.image = [UIImage imageNamed:@"售"];
    [self addSubview:sellImageView];
    self.sellImageView = sellImageView;
    
    UILabel *sellPriceTitleLabel = [UILabel yyr_labelWithText:@"售价：" fontSize:11 textColor:SECONDARY_TEXT_COLOR];
    [self addSubview:sellPriceTitleLabel];
    self.sellPriceTitleLabel = sellPriceTitleLabel;
    
    UILabel *sellPriceLabel = [UILabel yyr_labelWithText:@"" fontSize:11 textColor:MAIN_TEXT_COLOR];
    [self addSubview:sellPriceLabel];
    self.sellPriceLabel = sellPriceLabel;
    
    UILabel *sellRingRatioLabel = [UILabel yyr_labelWithText:@"" fontSize:8 textColor:MAIN_TEXT_COLOR ];
    [self addSubview:sellRingRatioLabel];
    self.sellRingRatioLabel = sellRingRatioLabel;
    
    UIImageView *rentImageView = [[UIImageView alloc] init];
    rentImageView.image = [UIImage imageNamed:@"租"];
    [self addSubview:rentImageView];
    self.rentImageView = rentImageView;
    
    UILabel *rentPriceTitleLabel = [UILabel yyr_labelWithText:@"租金：" fontSize:11 textColor:SECONDARY_TEXT_COLOR];
    [self addSubview:rentPriceTitleLabel];
    self.rentPriceTitleLabel = rentPriceTitleLabel;
    
    UILabel *rentPriceLabel = [UILabel yyr_labelWithText:@"" fontSize:11 textColor:MAIN_TEXT_COLOR];
    [self addSubview:rentPriceLabel];
    self.rentPriceLabel = rentPriceLabel;
    
    UILabel *rentRingRatioLabel = [UILabel yyr_labelWithText:@"" fontSize:8 textColor:MAIN_TEXT_COLOR ];
    [self addSubview:rentRingRatioLabel];
    self.rentRingRatioLabel = rentRingRatioLabel;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    CGSize buttonSize = CGSizeMake(44, 28);
    CGSize imageViewSize = CGSizeMake(16, 16);
    [self.currentRegionTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(2 * margin);
        make.left.equalTo(weakSelf).offset(2 * margin);
    }];
    
    [self.currentRegionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.currentRegionTitleLabel);
        make.left.equalTo(weakSelf.currentRegionTitleLabel.mas_right).offset(0.25 * margin);
    }];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.currentRegionTitleLabel.mas_bottom).offset(2 * margin);
        make.left.equalTo(weakSelf.currentRegionTitleLabel);
    }];
    
    [self.detailsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.currentRegionTitleLabel.mas_bottom).offset(0.25 * margin);
        make.right.equalTo(weakSelf).offset(-margin);
        make.size.mas_equalTo(buttonSize);
    }];
    
    [self.middleLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.countLabel.mas_bottom).offset(2 * margin);
        make.left.equalTo(weakSelf).offset(2 * margin);
        make.right.equalTo(weakSelf).offset(-2 * margin);
        make.height.mas_equalTo(1);
    }];
    
    [self.sellImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.currentRegionTitleLabel);
        make.top.equalTo(weakSelf.middleLine.mas_bottom).offset(2 * margin);
        make.size.mas_equalTo(imageViewSize);
        make.bottom.equalTo(weakSelf).offset(-2 * margin);
    }];
    
    [self.sellPriceTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.sellImageView);
        make.left.equalTo(weakSelf.sellImageView.mas_right).offset(margin);
    }];
    
    [self.sellPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.sellPriceTitleLabel);
        make.left.equalTo(weakSelf.sellPriceTitleLabel.mas_right);
    }];
    
    [self.sellRingRatioLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.sellPriceLabel);
        make.left.equalTo(weakSelf.sellPriceLabel.mas_right);
    }];
    
    [self.rentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_centerX).offset(margin);
        make.top.equalTo(weakSelf.middleLine.mas_bottom).offset(2 * margin);
        make.size.mas_equalTo(imageViewSize);
    }];
    
    [self.rentPriceTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.rentImageView);
        make.left.equalTo(weakSelf.rentImageView.mas_right).offset(margin);
    }];
    
    [self.rentPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.rentPriceTitleLabel);
        make.left.equalTo(weakSelf.rentPriceTitleLabel.mas_right);
    }];

    [self.rentRingRatioLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.rentPriceLabel);
        make.left.equalTo(weakSelf.rentPriceLabel.mas_right);
    }];
}

@end
